package com.ynet.middleground.user.service.impl;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.ynet.middleground.user.bean.QuerySMSLogNoPageReq;
import com.ynet.middleground.user.bean.QuerySMSLogReq;
import com.ynet.middleground.user.dto.QuerySMSLogDto;
import com.ynet.middleground.user.dto.SMSLogStatisticDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.user.bean.CheckSmsBalanceReq;
import com.ynet.middleground.user.bean.SendSmsReq;
import com.ynet.middleground.user.common.ErrorMessageConstant;
import com.ynet.middleground.user.dto.SendSmsInfoDTO;
import com.ynet.middleground.user.dto.SmsBalanceInfoDTO;
import com.ynet.middleground.user.model.SmsModel;
import com.ynet.middleground.user.service.SmsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * SmsServiceImpl 发短信服务
 * 
 * @author liulx
 * @date 2022/12/19 11:40
 */
@Api(value = "短信发送服务", tags = {"短信发送服务"})
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Path("sms")
@Service(timeout = 6000, version = "0.0.1")
public class SmsServiceImpl implements SmsService, GenericService {

    @Autowired
    private SmsModel smsModel;

    @POST
    @Path("send")
    @ApiOperation(value = "发送短信", notes = "发送短信")
    @ApiImplicitParam(name = "requestObj", dataType = "SendSmsReq", paramType = "body", required = true)
    @Override
    public ServiceResult<SendSmsInfoDTO> sendSms(SendSmsReq req) {
        ServiceResult<SendSmsInfoDTO> result = new ServiceResult<>();
        try {
            result.setResult(smsModel.sendSms(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "发送短信服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    @POST
    @Path("query")
    @ApiOperation(value = "查询短信账户余额", notes = "查询短信账户余额")
    @ApiImplicitParam(name = "requestObj", dataType = "CheckSmsBalanceReq", paramType = "body", required = true)
    @Override
    public ServiceResult<SmsBalanceInfoDTO> checkSmsBalance(CheckSmsBalanceReq req) {
        ServiceResult<SmsBalanceInfoDTO> result = new ServiceResult<>();
        try {
            result.setResult(smsModel.checkSmsBalance(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询短信账户余额服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    @POST
    @Path("query-sms-statistic")
    @ApiOperation(value = "查询短信调用统计(内管专用)", notes = "查询短信调用统计(内管专用)", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "QuerySMSLogReq", paramType = "body", required = true)
    @Override
    public ServiceResult<SMSLogStatisticDto> QuerySMSLogStatistic(QuerySMSLogReq req) {
        ServiceResult<SMSLogStatisticDto> result = new ServiceResult<>();
        try {
            result.setResult(smsModel.querySMSLogStatistic(req));
            result.setPagerInfo(req.getPagerInfo());
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询短信调用统计服务异常，case: {}",
                    Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;    }

    @POST
    @Path("query-sms-statistic-nopage")
    @ApiOperation(value = "查询短信调用统计不分页(内管专用)", notes = "查询短信调用统计不分页(内管专用)", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "QuerySMSLogNoPageReq", paramType = "body", required = true)
    @Override
    public ServiceResult<SMSLogStatisticDto> QuerySMSLogStatisticNoPage(QuerySMSLogNoPageReq req) {
        ServiceResult<SMSLogStatisticDto> result = new ServiceResult<>();
        try {
            result.setResult(smsModel.querySMSLogStatisticNoPage(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询短信调用统计服务异常，case: {}",
                    Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;    }
}
